package cn.melon.mod.sc;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.melon.common.ModelType;
import cn.melon.helper.BaseRequset;
import cn.melon.helper.IOnRequestListener;
import cn.melon.helper.LoggerHelper;
import cn.melon.helper.RequestHelper;
import cn.melon.view.admin.course.IUpdateCourseTable;
import cn.melon.view.admin.roll.RollInfoDialog;
import cn.melon.window.MainWindow;

/**
 * 与课程管理相关的请求类
 * 
 * @author corvo
 *
 */
public class ScRequest extends BaseRequset {

    private String TAG = LoggerHelper.makeLogTag(ScRequest.class);
    private Logger sctLogger = LoggerHelper.clientLogger();
    
    private MainWindow m;
    private IUpdateCourseTable updateCourseTable;
    
    public ScRequest() {
    }
    
    public ScRequest(MainWindow m) {
        this.m = m;
    }
    
    public ScRequest(IUpdateCourseTable updateCourseTable) {
        this.updateCourseTable = updateCourseTable;
    }

    @Override
    public void makeRequset() {
        
//        getAddInfoResult();                       // 添加课程信息(管理员)
//        getDeleteInfoResult();                    // 删除课程信息（管理员）
//        getGetInfoResult();                      // 获取某个课程的选课信息
//        getGetAllScInfoResult();                 // 获取所有课程选课信息
//        getChangeInfoResult();                   // 修改某课程信息（管理员）
//        getAddMessageResult();                   // 增加学生所选的某一门课程
//        getDeleteMessageResult();                // 删除学生所选的某一门课程
//        getSearchOneStudentResult();             // 查询某一个学生的课表
//        getGetAllInfoResult();                   // 查询某个学生课程的完整信息
//        getAddOneStudentResult();                // 添加一个学生的选课信息(管理员)
//        getDeleteOneStudentResult();             // 删除一个学生的选课信息(管理员)
//        getShowAllStudentResult();               // 查询所有学生的选课信息(管理员)
        //getGetAllScInfoResult();
        ScInfo info = new ScInfo();
        info.setClassName("微型机系统与接口技术");
        info.setClassNumber("4");
        info.setClassroom("九龙湖教四-104");
        info.setClassTeacher("杨全胜");
        info.setClassTerm("16-17-2");
        info.setClassTime("星期二-1-2,星期四-1-2");
        //addInfo(info);
        //changeInfoResult(info);
        //addMessage("1");
        //getSearchOneStudentResult();
        //getAllInfoResult();
        //deleteMessage("2");
        //getShowAllStudentResult();
//        getOneStudentResult("09014311");
        
    }

    /**
     * 添加课程信息
     * @param info 课程信息对象
     */
    public void addInfo(ScInfo info) {
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "ADDONECLASSINFO");
            jReq.put("content", info.toJSON());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.COURSE).addRequest(jReq.toString())
                .addReceiver("addinfo", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        sctLogger.debug(TAG + response);
                        try {
                            JSONObject jRes = new JSONObject(response);
                            if (jRes.getInt("code") == 200) {
                                JOptionPane.showMessageDialog(null, "添加课程信息成功");
                                getGetAllScInfoResult();
                            } else {
                                JOptionPane.showMessageDialog(null, "添加课程信息失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).build().start();
    }

    /**
     * 获取 删除课程信息结果
     * @Title: getDeleteInfoResult
     */
    public void getDeleteInfoResult() {
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "DELETEONECLASSINFO");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.COURSE).addRequest(jReq.toString())
                .addReceiver("deleteinfo", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        sctLogger.debug(TAG + response);
                        try {
                            JSONObject jRes = new JSONObject(response);
                            if (jRes.getInt("code") == 200) {
                                System.out.println("删除课程信息成功");

                            } else {
                                System.out.println("删除课程信息失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).build().start();
    }

    /**
     * 查询一门课程结果
     * @Title: getGetInfoResult
     */
    public void getGetInfoResult() {
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "ASKONECLASS");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.LIBRARY).addRequest(jReq.toString())
                .addReceiver("getinfo", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        sctLogger.debug(TAG + response);
                        List<ScInfo> infoList = new ArrayList<>();
                        try {
                            JSONObject jRes = new JSONObject(response);
                            if (jRes.getInt("code") == 200) {
                                JSONArray jContent = jRes.getJSONArray("content");
                                for (int i = 0; i < jContent.length(); i++) {

                                    ScInfo info = ScInfo.toInfo((JSONObject) jContent.get(i));
                                    infoList.add(info);
                                }
                            } else {
                                System.out.println("获取课程信息失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).build().start();
    }

    /**
     * 查询所有课程信息的返回结果
     * @Title: getGetAllScInfoResult
     * TODO 测试成功OK
     */
    public void getGetAllScInfoResult() {
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "ASKALLCLASSINFO");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.COURSE).addRequest(jReq.toString())
                .addReceiver("getallscinfo", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        sctLogger.debug(TAG + response);
                        List<ScInfo> infoList = new ArrayList<>();
                        try {
                            JSONObject jRes = new JSONObject(response);
                            if (jRes.getInt("code") == 200) {
                                JSONArray jContent = jRes.getJSONArray("content");
                                for (int i = 0; i < jContent.length(); i++) {
                                    ScInfo info = ScInfo.toInfo((JSONObject) jContent.get(i));
                                    infoList.add(info);
                                }
                                
                                if (m != null) {
                                    for (ScInfo info: infoList) {
                                        m.addLessonToChooseLessonWindow(info);
                                    }
                                } if (updateCourseTable != null) {
                                    updateCourseTable.updateTable(infoList);
                                }
                                
                            } else {
                                System.out.println("获取所有课程信息失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).build().start();
    }

    /**
     * 修改一门课程 
     * @param info 课程信息
     */
    public void changeInfo(ScInfo info) {
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "CHANGEONECLASSINFO");
            jReq.put("content", info.toJSON());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.COURSE).addRequest(jReq.toString())
                .addReceiver("changeinfo", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        sctLogger.debug(TAG + response);
                        try {
                            JSONObject jRes = new JSONObject(response);
                            if (jRes.getInt("code") == 200) {
                                JOptionPane.showMessageDialog(null, "修改课程信息成功");
                                getGetAllScInfoResult();
                            } else {
                                JOptionPane.showMessageDialog(null, "修改课程信息失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).build().start();
    }
    
    /**
     * 删除某一个学生弃选的一门课
     * @param classNumber 课程号信息
     */
    public void deleteMessage(String classNumber) {
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "DELETEONESTUDENTMESSAGE");
            jReq.put("content", classNumber);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.COURSE).addRequest(jReq.toString())
                .addReceiver("deletemessage", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        sctLogger.debug(TAG + response);
                        try {
                            JSONObject jRes = new JSONObject(response);
                            if (jRes.getInt("code") == 200) {
                                System.out.println("删除学生信息成功");

                            } else {
                                System.out.println("删除学生信息失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).build().start();
    }

    /**
     * 添加某一个学生新选的一门课
     * @param classNumber 课程代号
     */
    public void addMessage(String classNumber) {
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "ADDONESTUDENTMESSAGE");
            jReq.put("content", classNumber);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.COURSE).addRequest(jReq.toString())
                .addReceiver("addmessage", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        sctLogger.debug(TAG + response);
                        try {
                            JSONObject jRes = new JSONObject(response);
                            if (jRes.getInt("code") == 200) {
                                JOptionPane.showMessageDialog(null, "增加课程成功");
                            } else {
                                JOptionPane.showMessageDialog(null, jRes.get("content"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).build().start();
    }

    /**
     * 查询一个学生所选的所有课的返回结果, 只有课程号码
     * @param studentNumber 学生学号
     */
    public void getOneStudentResult(String studentNumber,RollInfoDialog dlg) {
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "ASKONESTUDENTINFO");
            jReq.put("content", studentNumber);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.COURSE).addRequest(jReq.toString())
                .addReceiver("askonestudent", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        sctLogger.debug(TAG + response);
                        try {
                            JSONObject jRes = new JSONObject(response);
                            if (jRes.getInt("code") == 200) {
                                System.out.println(jRes.getString("content"));
                                JSONObject content = jRes.getJSONObject("content");
                                dlg.setSelectCourse(content.getString("classTable"));
                            } else {
                                System.out.println("查询课表失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).build().start();
    }

    /**
     * 查询一个学生所选的所有课以及课程信息的返回结果
     * @Title: getGetAllInfoResult
     */
    public void getAllInfoResult() {
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "ASKONESTEDENTALLINFO");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.COURSE).addRequest(jReq.toString())
                .addReceiver("askonestudentall", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        sctLogger.debug(TAG + response);
                        List<ScInfo> infoList = new ArrayList<>();
                        try {
                            JSONObject jRes = new JSONObject(response);
                            if (jRes.getInt("code") == 200) {
                                JSONArray jContent = jRes.getJSONArray("content");
                                for (int i = 0; i < jContent.length(); i++) {
                                    ScInfo info = ScInfo.toInfo((JSONObject) jContent.get(i));
                                    infoList.add(info);
                                }

                                if (m != null) {
                                    for(ScInfo info: infoList) {
                                        m.addLessonToTable(info);
                                    }
                                }
                            } else {
                                System.out.println("查询课表失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).build().start();
    }

    /**
     * 添加一个学生的选课信息的结果
     * @Title: getAddOneStudentResult
     */
    public void getAddOneStudentResult() {
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "ADDONESTUDENT");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.COURSE).addRequest(jReq.toString())
                .addReceiver("addstudent", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        sctLogger.debug(TAG + response);
                        List<StInfo> infoList = new ArrayList<>();
                        try {
                            JSONObject jRes = new JSONObject(response);
                            if (jRes.getInt("code") == 200) {
                                System.out.println("添加学生信息成功");

                            } else {
                                System.out.println("添加学生信息失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).build().start();
    }

    /**
     * 删除一个学生选课信息
     * @param studentNumber 学生学号
     */
    public void deleteOneStudentResult(String studentNumber) {
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "DELETEONESTUDENT");
            jReq.put("content", studentNumber);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.COURSE).addRequest(jReq.toString())
                .addReceiver("deletestudent", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        sctLogger.debug(TAG + response);
                        try {
                            JSONObject jRes = new JSONObject(response);
                            if (jRes.getInt("code") == 200) {
                                System.out.println("删除学生信息成功");

                            } else {
                                System.out.println("删除学生信息失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).build().start();
    }

    /**
     * 获取所有学生的选课信息的结果
     * @Title: getShowAllStudentResult
     */
    public void getShowAllStudentResult() {
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "ASKALLSTUDENT");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.COURSE).addRequest(jReq.toString())
                .addReceiver("showallstudent", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        sctLogger.debug(TAG + response);
                        List<StInfo> infoList = new ArrayList<>();
                        try {
                            JSONObject jRes = new JSONObject(response);
                            if (jRes.getInt("code") == 200) {
                                JSONArray jContent = jRes.getJSONArray("content");
                                for (int i = 0; i < jContent.length(); i++) {

                                    StInfo info = StInfo.toInfo((JSONObject) jContent.get(i));
                                    infoList.add(info);
                                }
                            } else {
                                System.out.println("获取课程信息失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).build().start();
    }

}
